<!DOCTYPE  HTML>
<html>
	<head>
		
		<script type="text/javascript" src="../resources/script.js"></script>
		<meta charset="utf-8">
  		<meta name="viewport" content="width=device-width, initial-scale=1">
  		<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  		<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  		<link rel="stylesheet" href="../resources/style.css" />
	</head>
	<body>
<script>
$(document).ready(function(){
  $("#btnaddNewTechnology").click(function(){
    $("#divTech").hide();
  });
});

$(document).ready(function(){
  $("#btnaddTech").click(function(){
    $("#divTech").show();
  });
});

</script>
		<?php
			foreach (glob("../model/*.php") as $filename)
                        {
                                include_once $filename;
                        }

                        foreach (glob("../dao/*.php") as $filename)
                        {
                                include_once $filename;
                        }

			$techdao = new TechnologyDAOImpl();
			
			/*array , Technology -> boolean
                        purpose. produces true if the given Technology is found in the given array of Technology, false other wise*/
                        function found($tech_list , $tech)
                        {
                                $flag = false;

                                foreach($tech_list as $tech_in_list)
                                {
                                        if(($tech_in_list->getTechName() == $tech->getTechName()))
                                        {
                                                $flag = true;
                                                break;
                                        }
                                }
				
				return $flag;
                        }


			if(array_key_exists('tech_to_del', $_GET))
			{
				if(!$techdao->removeTech($_GET['tech_to_del']))
				{
					echo 'some thing went wrong';
				}
				else
				{
					array_splice($_GET, 0 , 1);
				}
			}

			if($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$tech_name = $_POST['tnm'];
				$desc	   = $_POST['tdesc'];

				$techdao   = new TechnologyDAOImpl();
				$tech      = new Technology($tech_name, $desc);

                                $tech_list = $techdao->listTechnology()->getArrayList();
                                if(isset($tech_list))
                                {
                                        if(!found($tech_list, $tech))
                                        {
                                                $techdao->saveOrUpdate($tech);
                                                //echo "!!!sessn added successfully";
                                        }
                                        else
                                        {
                                                echo "!!!tech already exists";
                                        }
                                }
                                else
                                {
                                        $techdao->saveOrUpdate($tech);
                                        echo "!!!tech added successfully";
                                }

			}

		?>
		<div id="ahmainpnl" >
			<img id="ahlogo" src="../resources/logo.png"></img>
			<div id="ahheaderpnl">
				<h2 id="hSess">Technology</h2>
				<button id="btnaddNewTechnology" type="button"  class="btn btn-primary" data-toggle="collapse" data-target="#addnewTech" >Add Technology</button>
			</div>
			<div id="ahmenupnl">
				 <a href="home_admin.php" id="ahbtnhome" class="btn btn-success">Home</a>
				<!--button type="button" id="ahbtnhome" class="btn btn-success" onclick="window.open('home_admin.html')">Home</button-->
				<hr/>
				<a href="addUser.php" id="ahbtnhome" class="btn btn-info">Users</a>
				<!--button type="button" id="ahbtnuser" class="btn btn-info" onclick="window.open('addUser.html')">Users</button-->
				<a href="addTechnology.php" id="ahbtnhome" class="btn btn-info">Technologies</a>
				<!--button type="button" id="ahbtntech" class="btn btn-info" onclick="window.open('addTechnology.html')">Technologies</button-->
				<a id="btnlogout">Logout</a>
			</div>
			<div id="ahcontentpnl">
				<div class="container" >
  					
					
				</div>
				<div id="addnewTech"class="collapse container" >
				<form role="form" id="addtechform" name="addtechform" action='' method='POST'>
  						<div class="form-group">
    					<table>
  							<tr>
  								<td><input type="text" class="form-control" name="tnm" id="tnm" placeholder="Name"></td>
  							</tr>
  							<tr>
  								<td colspan="2"><input type="text" class="form-control" name="tdesc" id="tdesc" placeholder="Description"></td>
  							</tr>
  						</table>
  					</div>
  					 <button  id="btnaddTech" type="submit"  class="btn btn-default">Add Technology</button>
					</form>
 

				
				</div>
			<div id="divTech">
 				<table class="table table-hover">
        				<thead>
          					<tr>
            						<th>ID</th>
	    						<th>Name</th>
							<th>Description</th>
          					</tr>
        				</thead>
        				<tbody>
						<?php
							$techdao = new TechnologyDAOImpl();
							$tech_list = $techdao->listTechnology()->getArrayList();

							if(isset($tech_list))
							{
								foreach($tech_list as $tech)
								{
						?>
         					 <tr>
							<td><?php print $tech->getId(); ?></td>
							<td><?php print $tech->getTechName(); ?></td>
							<td>
								<?php
									$desc = $tech->getDesc();
									if($desc == null)
									{
										print "'------------------------------------'";
									}
									else
									{
										print $desc;
									}
								?>
							</td>
							<td>
								<button type="button" class="btn btn-danger" onclick="window.open('addTechnology.php?tech_to_del=<?php print $tech->getId(); ?>','_self')">Delete</button>

							</td>
					          </tr>
							<?php
								}
							}
							else
							{
								print 'There are no technology Add some Technology';
							}
							?>
        				</tbody>
      				</table>
			</div>

		
			
		</div>
		<div class="divfooter" >
			<p>© 2007-2015 Spring Computing Technologies Pvt. Ltd., All Rights Reserved.</p>
		</div>
	</body>
	
</html>
